package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.DmcRfidEventOldmanMapper;
import com.csun.cmny.provider.exceptions.DmcBizException;
import com.csun.cmny.provider.model.domain.DmcRfidEventOldman;
import com.csun.cmny.provider.model.dto.rfid.RfidEventOldmanDealDto;
import com.csun.cmny.provider.model.dto.rfid.RfidEventOldmanQueryDto;
import com.csun.cmny.provider.model.vo.rfid.DmcRfidEventOldmanVo;
import com.csun.cmny.provider.model.vo.rfid.DmcRfidLabelVo;
import com.csun.cmny.provider.model.vo.rfid.DmcRfidReaderVo;
import com.csun.cmny.provider.model.vo.nursing.NursingGroupUserVo;
import com.csun.cmny.provider.service.DmcRfidEventOldmanService;
import com.csun.cmny.provider.service.OmcRpcService;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-08-24 10:14
 * @Description: the class dmc rfid event oldman service impl
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DmcRfidEventOldmanServiceImpl extends BaseService<DmcRfidEventOldman>
        implements DmcRfidEventOldmanService {

    @Resource
    private OmcRpcService omcRpcService;
    @Resource
    private DmcRfidEventOldmanMapper dmcRfidEventOldmanMapper;

    /**
     * select the last times rfid reader one of oldman
     *
     * @param dmcRfidReaderVo the dmc rfid reader vo
     * @param dmcRfidLabelVo  the dmc rfid label vo
     * @return the dmc rfid event oldman vo
     */
    @Override
    public DmcRfidEventOldmanVo selectFirstRfidEvent(DmcRfidReaderVo dmcRfidReaderVo, DmcRfidLabelVo dmcRfidLabelVo) {

        RfidEventOldmanQueryDto rfidEventOldmanQueryDto = new RfidEventOldmanQueryDto();
        rfidEventOldmanQueryDto.setOrderBy("rfid_event_oldman.created_time desc");
        rfidEventOldmanQueryDto.setGroupId(dmcRfidLabelVo.getGroupId());
        rfidEventOldmanQueryDto.setOldmanId(dmcRfidLabelVo.getOldmanId().intValue());
        rfidEventOldmanQueryDto.setLabelId(dmcRfidLabelVo.getId());
        rfidEventOldmanQueryDto.setNumber(0);
        rfidEventOldmanQueryDto.setPageNum(1);
        rfidEventOldmanQueryDto.setPageSize(1);

        List<DmcRfidEventOldmanVo> dmcRfidEventOldmanVoList = queryListWithPage(rfidEventOldmanQueryDto);
        if (dmcRfidEventOldmanVoList == null || dmcRfidEventOldmanVoList.size() < 1) {
            return null;
        }

        return dmcRfidEventOldmanVoList.get(0);
    }

    /**
     * query dmc rfid event oldman list with page
     *
     * @param rfidEventOldmanQueryDto the rfid event oldman query dto
     *
     * @return the list
     */
    @Override
    public List<DmcRfidEventOldmanVo> queryListWithPage(RfidEventOldmanQueryDto rfidEventOldmanQueryDto) {

        PageHelper.startPage(rfidEventOldmanQueryDto.getPageNum(), rfidEventOldmanQueryDto.getPageSize());

        return dmcRfidEventOldmanMapper.queryListWithPage(rfidEventOldmanQueryDto);
    }

    /**
     * deal rfid event oldman out notify
     *
     * @param rfidEventOldmanDealDto the rfid event oldman deal dto
     * @param loginAuthDto the login auth dto
     *
     * @return the int.
     */
    @Override
    public Integer dealOldmanOutNotify(RfidEventOldmanDealDto rfidEventOldmanDealDto, LoginAuthDto loginAuthDto) {

        // 查询老人外出Id记录是否存在
        DmcRfidEventOldman dmcRfidEventOldman = dmcRfidEventOldmanMapper
                .selectByPrimaryKey(rfidEventOldmanDealDto.getId());
        if (dmcRfidEventOldman == null) {
            throw new DmcBizException(ErrorCodeEnum.DMC10066001);
        }

        // 校验是否为用一个班组
        NursingGroupUserVo nursingGroupUserVo = omcRpcService.queryNursingGroupUserByUserId(loginAuthDto.getUserId());
        if (nursingGroupUserVo == null || nursingGroupUserVo.getNursingGroupId() == null) {
            throw new DmcBizException(ErrorCodeEnum.DMC10066002);
        }

        if (!nursingGroupUserVo.getNursingGroupId().equals(dmcRfidEventOldman.getNursingGroupId())) {
            throw new DmcBizException(ErrorCodeEnum.DMC10066003);
        }

        dmcRfidEventOldman.setUpdateInfo(loginAuthDto);
        dmcRfidEventOldman.setUserId(loginAuthDto.getUserId());
        dmcRfidEventOldman.setUserName(loginAuthDto.getUserName());
        dmcRfidEventOldman.setDealStat(1);
        dmcRfidEventOldman.setDealResult(rfidEventOldmanDealDto.getDealResult());

        return dmcRfidEventOldmanMapper.updateByPrimaryKeySelective(dmcRfidEventOldman);
    }

    /**
     * delete by label id
     *
     * @param deviceId the device id (rfid label id)
     *
     * @return the int.
     */
    @Override
    public Integer deleteByLabelId(Long deviceId) {

        return dmcRfidEventOldmanMapper.deleteByLabelId(deviceId);
    }

    /**
     * delete by reader id
     *
     * @param deviceId the device id (rfid reader id)
     *
     * @return the int.
     */
    @Override
    public Integer deleteByReaderId(Long deviceId) {

        return dmcRfidEventOldmanMapper.deleteByReaderId(deviceId);
    }
}
